package com.cash.lemoloan.statisticlib.Utils;

import static android.content.Context.SENSOR_SERVICE;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Build;
import android.text.TextUtils;

import com.cash.lemoloan.statisticlib.StatisticApplication;


public class SimulatorUtil {

    /**
     * 判断蓝牙是否有效来判断是否为模拟器
     *
     * @return true 为模拟器
     */
    public static boolean notHasBlueTooth() {
        BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
        if (ba == null) {
            return true;
        } else {
            // 如果有蓝牙不一定是有效的。获取蓝牙名称，若为null 则默认为模拟器
            String name = ba.getName();
            if (TextUtils.isEmpty(name)) {
                //LogUtil.d("SimulatorUtil", "notHasBlueTooth true");
                return true;
            } else {
                //LogUtil.d("SimulatorUtil", "notHasBlueTooth false");
                return false;
            }
        }
    }

    /**
     * 判断是否存在光传感器来判断是否为模拟器
     * 部分真机也不存在温度和压力传感器。其余传感器模拟器也存在。
     *
     * @return true 为模拟器
     */
    public static Boolean notHasLightSensorManager(Context context) {
        SensorManager sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
        Sensor sensor8 = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT); //光
        if (null == sensor8) {
           // LogUtil.d("SimulatorUtil", "notHasLightSensorManager true");
            return true;
        } else {
           // LogUtil.d("SimulatorUtil", "notHasLightSensorManager false");
            return false;
        }
    }

    /**
     * 根据部分特征参数设备信息来判断是否为模拟器
     *
     * @return true 为模拟器
     */
    public static boolean isFeatures() {
        boolean isFeatures = Build.FINGERPRINT.startsWith("generic")
                || Build.FINGERPRINT.toLowerCase().contains("vbox")
                || Build.FINGERPRINT.toLowerCase().contains("test-keys")
                || Build.MODEL.contains("google_sdk")
                || Build.MODEL.contains("Emulator")
                || Build.MODEL.contains("Android SDK built for x86")
                || Build.MANUFACTURER.contains("Genymotion")
                || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
                || "google_sdk".equals(Build.PRODUCT);
        //LogUtil.d("SimulatorUtil", "isFeatures = " + isFeatures);

        return isFeatures;
    }

    /**
     * 判断cpu是否为电脑来判断 模拟器
     *
     * @return true 为模拟器
     */
    public static boolean checkIsNotRealPhone() {
        try {
            String serial = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                if (StatisticApplication.getInstance().checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager
                        .PERMISSION_GRANTED) {
                    serial = Build.getSerial();
                }
            } else {
                serial = Build.SERIAL;
            }
            //LogUtil.d("SimulatorUtil", "checkIsNotRealPhone:" + serial);
            if ("unknown".equals(serial) || "android".equals(serial)) {
                return true;
            }
            return false;
        }catch (Exception e){
            return false;
        }
    }
    /**
     * 是否模拟器
     *
     * @return
     */
    public static boolean isSimulator(){
        if (!SimulatorUtil.checkIsNotRealPhone() &&
                !SimulatorUtil.notHasLightSensorManager(StatisticApplication.getInstance())
                && !SimulatorUtil.isFeatures()) {
            return false;
        } else {
            return true;
        }
    }

}
